#include "stm32f4xx_hal.h"
#include "stm32f4xx_ll_rcc.h"
#include "stdio.h"
#include "log.h"

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"

#include "DevicesQueue.h"
#include "DriverShell.h"
#include "DriverNetwork.h"

#include "taskMessageRead.h"


/* 电流监控任务句柄 */
TaskHandle_t g_TaskMessageReadHand = NULL;

#define MESSAGE_READ_BUFF_LENGTH 1024
static uint8_t st_TaskMessageReadBuff[MESSAGE_READ_BUFF_LENGTH + 4] = {0};


void vTaskMessageRead(void *pvParameters)
{
    uint32_t uiNotifiedValue = 0;
    int32_t iLength = 0;

    while(1)
    {
        ulTaskNotifyTake(pdTRUE, portMAX_DELAY);

        /* USB 接收到的数据 */
        while((iLength = iQueueGetLengthOfOccupyNeed(&g_TypeQueueUSBRead, FRAME_SEPARETOR)) > 0)
        {
            iLength = ((iLength > MESSAGE_READ_BUFF_LENGTH) ? MESSAGE_READ_BUFF_LENGTH : iLength);

            enumQueuePopDatas(&g_TypeQueueUSBRead, st_TaskMessageReadBuff, iLength);

            cShellParserJson(st_TaskMessageReadBuff, NETWORK_CHANNEL_HARD_USB);
        }

        /* Uart Log 接收到的数据 */
        while((iLength = iQueueGetLengthOfOccupyNeed(&g_TypeQueueUartLogRead, FRAME_SEPARETOR)) > 0)
        {
            iLength = ((iLength > MESSAGE_READ_BUFF_LENGTH) ? MESSAGE_READ_BUFF_LENGTH : iLength);

            enumQueuePopDatas(&g_TypeQueueUartLogRead, st_TaskMessageReadBuff, iLength);

            cShellParserJson(st_TaskMessageReadBuff, NETWORK_CHANNEL_HARD_LOG);
        }

        /* Uart Soc 接收到的数据 */
        while((iLength = iQueueGetLengthOfOccupyNeed(&g_TypeQueueUartSocRead, FRAME_SEPARETOR)) > 0)
        {
            iLength = ((iLength > MESSAGE_READ_BUFF_LENGTH) ? MESSAGE_READ_BUFF_LENGTH : iLength);

            enumQueuePopDatas(&g_TypeQueueUartSocRead, st_TaskMessageReadBuff, iLength);

            cShellParserJson(st_TaskMessageReadBuff, NETWORK_CHANNEL_HARD_SOC);
        }

        /* Uart Mcu 接收到的数据 */
        while((iLength = iQueueGetLengthOfOccupyNeed(&g_TypeQueueUartUpperRead, FRAME_SEPARETOR)) > 0)
        {
            iLength = ((iLength > MESSAGE_READ_BUFF_LENGTH) ? MESSAGE_READ_BUFF_LENGTH : iLength);

            enumQueuePopDatas(&g_TypeQueueUartUpperRead, st_TaskMessageReadBuff, iLength);

            cShellParserJson(st_TaskMessageReadBuff, NETWORK_CHANNEL_HARD_UPPER);
        }
    }
}
